import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/class_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/widget/list/notice_item.dart';
import 'package:FineArt/page/my/classdetail.dart';
import 'package:FineArt/page/my/createclass.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:FineArt/utils/sputils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

//班级列表
class ClassListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ClassListPageState();
  }
}

class _ClassListPageState extends State<ClassListPage> {
  List resources = [6];

  @override
  void initState() {
    super.initState();
    _loadClass();
  }

  List<TeacherClassesEntity> data = [];

  _loadClass() {
    String uid = SPUtils.getUID();
    ClassApi().teacherclasses(uid).then((res) {
      if (res.code == 200) {
        setState(() {
          data = res.data;
        });
      } else {
        ToastUtils.toast("获取班级出错${res.msg}");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text(
              "我的班级",
              style: TextStyle(color: text_black, fontSize: setSp(18)),
            ),
            centerTitle: true,
            elevation: 0.5,
            backgroundColor: Colors.white,
            brightness: Brightness.light,
            toolbarHeight: getPortraitSHeight(40),
            leading: IconButton(
                alignment: Alignment.centerLeft,
                icon: Image.asset(
                  "assets/images/icon_back_black.png",
                  width: getPortraitSWidth(11),
                  height: getPortraitSHeight(18),
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                })),
        body: SafeArea(
            child: Stack(
          alignment: Alignment.center,
          children: [
            _widget_refresh_list(),
            Positioned(
              child: buildCreateClass(),
              bottom: 40,
            )
          ],
        )));
  }

  EasyRefreshController _controller = EasyRefreshController();

  Widget _widget_refresh_list() {
    double width = MediaQuery.of(context).size.width * 0.98;
    MaterialHeader header =
        MaterialHeader(completeDuration: new Duration(milliseconds: 100));
    return EasyRefresh.custom(
      controller: _controller,
      header: header,
      footer: MaterialFooter(),
      onRefresh: () async {},
      onLoad: () async {
        // _nextPage(widget.currentPath, widget.currentFilter);
      },
      slivers: <Widget>[
        // SliverToBoxAdapter(
        //   child: _playHeaderCol(isTeacher),
        // ),
        SliverPadding(
            padding: EdgeInsets.only(left: 3, right: 3),
            sliver: SliverList(
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  //创建子widget
                  // var resource = resources[index];
                  return buildClassItem(0, data[index].className, index);
                },
                childCount: data.length,
              ),
            )),

        //=====列表=====//
      ],
    );
  }

  Widget buildClassItem(int total, String name, int index) {
    return InkWell(
      child: Container(
        color: Colors.white,
        child: Padding(
          padding: EdgeInsets.symmetric(
              vertical: getPortraitSHeight(14.5),
              horizontal: getPortraitSWidth(28)),
          child: Column(
            children: [
              Row(
                children: [
                  Container(
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Colors.redAccent,
                      borderRadius: BorderRadius.all(Radius.circular(2.0)),
                    ),
                    child: Text(
                      "班",
                      style:
                          TextStyle(fontSize: setSp(12), color: Colors.white),
                    ),
                    width: getPortraitSWidth(16),
                    height: getPortraitSHeight(16),
                  ),
                  SizedBox(
                    width: getPortraitSWidth(12.5),
                  ),
                  Text(
                    name + (total > 0 ? "($total)" : ""),
                    style: TextStyle(fontSize: setSp(15), color: text_black),
                  ),
                  Expanded(child: Text("")),
                  Image.asset(
                    "assets/images/icon_right_arrow.png",
                    width: getPortraitSWidth(8),
                    height: getPortraitSHeight(16),
                  ),
                ],
              ),
              // SizedBox(height: 12,),
              // Divider(height: 0.5,color: Colors.grey,)
            ],
          ),
        ),
      ),
      onTap: () {
        LogUtil.d("class click $index");
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return ClassDetailPage(data[index]);
        }));
      },
    );
  }

  Widget buildCreateClass() {
    return InkWell(
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return CreateClassPage();
        }));
      },
      child: Container(
        width: SizeConfig.screenWidth * 0.9,
        height: getPortraitSHeight(50),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: red_main,
            borderRadius: BorderRadius.all(Radius.circular(25))),
        margin:
            EdgeInsets.symmetric(vertical: 0, horizontal: getPortraitSWidth(0)),
        child: Text(
          "创建班级",
          style: TextStyle(color: Colors.white, fontSize: 15),
        ),
      ),
    );
  }
}
